package com.ruoyi.common.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncryptUtil {

    public static String sign(String str, String type){
        String s=Encrypt(str,type);
        return s;
    }

    public static String Encrypt(String strSrc, String encName) {
        MessageDigest md = null;
        String strDes = null;
        byte[] bt = strSrc.getBytes();
        try {
            md = MessageDigest.getInstance(encName);
            md.update(bt);
            strDes = bytes2Hex(md.digest()); // to HexString
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return null;
        }
        return strDes;
    }

    public static String bytes2Hex(byte[] bts) {
        String des = "";
        String tmp = null;
        for (int i = 0; i < bts.length; i++) {
            tmp = (Integer.toHexString(bts[i] & 0xFF));
            if (tmp.length() == 1) {
                des += "0";
            }
            des += tmp;
        }
        return des;
    }


    public static String MD5(String str) {

        return sign(str,"MD5");
    }

    public static String SHA1(String str) {

        return sign(str,"SHA-1");
    }

    public static String cycleEncryption(String password,Integer times) {
        for(int i=0;i<times;i++) {
            password = MD5(SHA1(password));
        }
        return password;
    }

    public static void main(String[] args) {
        System.out.println(cycleEncryption("123456",1));
    }
}
